<?php

namespace Phones\MainBundle\Entity;

use Symfony\Component\Translation\Translator,
    Symfony\Component\Translation\Loader\YamlFileLoader;

abstract class BaseEntity {
    protected $params;
    protected $container;
    private $translator;

    public function setContainer($container) {
        $this->container = $container;
    }

    public function getParameter($name) {
        $this->checkContainer();

        return $this->container->getParameter($name);
    }

    public function translate($string) {
        $this->checkContainer();

        if (! $this->translator) {
            $this->loadTranslator();
        }

        return $this->translator->trans($string);
    }

    private function checkContainer() {
        if (! $this->container) {
            throw new Exception('Container not set.');
        }
    }

    private function loadTranslator() {
        $root = dirname($this->container->getParameter('kernel.root_dir'));
        $locale     = $this->getParameter('locale');
        $path       = "{$root}/src/Phones/MainBundle/Resources/translations/messages.{$locale}.yml";
        
        $this->translator = new Translator($locale);
        $this->translator->addLoader('yaml', new YamlFileLoader());
        $this->translator->addResource('yaml', $path, $locale);
    }
}